package com.hiultra.archive.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.hiultra.archive.domain.ArcArchive;
import com.hiultra.archive.domain.SysExamineConfig;
import com.hiultra.archive.service.ISysExamineConfigService;
import com.hiultra.common.constant.UserConstants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hiultra.common.annotation.Log;
import com.hiultra.common.core.controller.BaseController;
import com.hiultra.common.core.domain.AjaxResult;
import com.hiultra.common.enums.BusinessType;
import com.hiultra.archive.domain.SimfExamine;
import com.hiultra.archive.service.ISimfExamineService;
import com.hiultra.common.utils.poi.ExcelUtil;
import com.hiultra.common.core.page.TableDataInfo;

/**
 * 审批Controller
 *
 * @author ls
 * @date 2023-09-25
 */
@RestController
@RequestMapping("/examine/examine")
public class SimfExamineController extends BaseController {
    @Autowired
    private ISimfExamineService simfExamineService;
    @Autowired
    private ISysExamineConfigService sysExamineConfigService;

    /**
     * 查询审批列表
     */
    @PreAuthorize("@ss.hasPermi('examine:examine:list')")
    @GetMapping("/list")
    public TableDataInfo list(SimfExamine simfExamine) {
        startPage();
        List<SimfExamine> list = simfExamineService.selectSimfExamineList(simfExamine);
        return getDataTable(list);
    }

    /**
     * 导出审批列表
     */
    @PreAuthorize("@ss.hasPermi('examine:examine:export')")
    @Log(title = "审批", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SimfExamine simfExamine) {
        List<SimfExamine> list = simfExamineService.selectSimfExamineList(simfExamine);
        ExcelUtil<SimfExamine> util = new ExcelUtil<SimfExamine>(SimfExamine.class);
        util.exportExcel(response, list, "审批数据");
    }

    /**
     * 获取审批详细信息
     */
    @PreAuthorize("@ss.hasPermi('examine:examine:query')")
    @GetMapping(value = "/{examineId}")
    public AjaxResult getInfo(@PathVariable("examineId") Long examineId) {
        return AjaxResult.success(simfExamineService.selectSimfExamineByExamineId(examineId));
    }

    /**
     * 新增审批
     */
    @PreAuthorize("@ss.hasPermi('examine:examine:add')")
    @Log(title = "审批", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SimfExamine simfExamine) {
        if (UserConstants.NOT_UNIQUE.equals(simfExamineService.checkExamineCodeUnique(simfExamine))) {
            return AjaxResult.error("新增流程编号'" + simfExamine.getExamineCode() + "'失败，流程编号已存在");
        }
        if (UserConstants.NOT_UNIQUE.equals(simfExamineService.checkExamineNameUnique(simfExamine))) {
            return AjaxResult.error("新增流程名称'" + simfExamine.getExamineName() + "'失败，流程名称已存在");
        }
        return toAjax(simfExamineService.insertSimfExamine(simfExamine));
    }

    /**
     * 修改审批
     */
    @PreAuthorize("@ss.hasPermi('examine:examine:edit')")
    @Log(title = "审批", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SimfExamine simfExamine) {
        if (UserConstants.NOT_UNIQUE.equals(simfExamineService.checkExamineCodeUnique(simfExamine))) {
            return AjaxResult.error("修改流程编号'" + simfExamine.getExamineCode() + "'失败，流程编号已存在");
        }
        if (UserConstants.NOT_UNIQUE.equals(simfExamineService.checkExamineNameUnique(simfExamine))) {
            return AjaxResult.error("修改流程名称'" + simfExamine.getExamineName() + "'失败，流程名称已存在");
        }
        return toAjax(simfExamineService.updateSimfExamine(simfExamine));
    }

    /**
     * 删除审批
     */
    @PreAuthorize("@ss.hasPermi('examine:examine:remove')")
    @Log(title = "审批", businessType = BusinessType.DELETE)
    @DeleteMapping("/{examineIds}")
    public AjaxResult remove(@PathVariable Long[] examineIds) {

        for (Long examineId : examineIds) {
            //是否被引用
            List<SysExamineConfig> sysExamineConfigList = sysExamineConfigService.selectSysExamineConfigList(new SysExamineConfig() {{
                setExamineId(examineId);
            }});
            if (sysExamineConfigList.size() > 0) {
                return AjaxResult.error("审批流程序号" + examineId + "被引用，无法删除！");
            }
        }

        return toAjax(simfExamineService.deleteSimfExamineByExamineIds(examineIds));
    }
}
